﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 窗口切换工具
{
    public partial class Form1 : Form
    {

        const string const_UptimeRateICOName = "UptimeRateICO";
        const string const_KingviewICOName = "Kingview";
        const string const_ReportSystemICOName = "RptICO";
        const string const_FaultAnalysICOName = "analys";
        const string const_DesktopICOName = "DesktopICO";
        public Form1()
        {
            this.TopMost = true;
            this.TopLevel = true;
            User32.SetWindowPos(Handle, 0, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
            this.Location = new Point(DefShowWindowLocation.X + 100, DefShowWindowLocation.Y);
            InitializeComponent();

            ImageList imageList = new ImageList();
            imageList.Images.Add(const_UptimeRateICOName, global::窗口切换工具.Properties.Resources.Pie_Chart);
            imageList.Images.Add(const_KingviewICOName, global::窗口切换工具.Properties.Resources.MachineGViewer);
            imageList.Images.Add(const_ReportSystemICOName, global::窗口切换工具.Properties.Resources.presentData);
            imageList.Images.Add(const_FaultAnalysICOName, global::窗口切换工具.Properties.Resources.analys);
            imageList.Images.Add(const_DesktopICOName, global::窗口切换工具.Properties.Resources.桌面);
            imageList.ImageSize = new Size(50, 50);
            imageList.ColorDepth = ColorDepth.Depth32Bit;

            t1_切换组态王ToolStripMenuItem.Image = imageList.Images[const_KingviewICOName];
            t2_切换报表查看器ToolStripMenuItem.Image = imageList.Images[const_ReportSystemICOName];
            t3_开动率管理系统ToolStripMenuItem.Image = imageList.Images[const_UptimeRateICOName];
            t4_故障分析程序ToolStripMenuItem.Image = imageList.Images[const_FaultAnalysICOName];
            t5桌面ToolStripMenuItem.Image= imageList.Images[const_DesktopICOName];

            //this.BackColor = Color.Transparent;

            menuStrip1.SuspendLayout();
            menuStrip1.Items.Clear();
            menuStrip1.Items.AddRange(new ToolStripItem[] {
            t1_切换组态王ToolStripMenuItem,
            t2_切换报表查看器ToolStripMenuItem,
            t3_开动率管理系统ToolStripMenuItem,
            t4_故障分析程序ToolStripMenuItem,
            t5桌面ToolStripMenuItem
            }) ;
            menuStrip1.ResumeLayout();
            log.AppendLine(DateTime.Now + " 窗口资源载入完成");
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
        public Point DefHideWindowLocation = new Point(-253, 270);
        public Point DefShowWindowLocation = new Point(0, 270);
        private void Form1_Load(object sender, EventArgs e)
        {
            Regex rege = new Regex("[\\w.\\s\\u4e00-\\u9fa5]{0,999}");
            wndName = System.Configuration.ConfigurationManager.AppSettings["wndNameArray"].Split(',');
            for (int i = 0; i < wndName.Length; i++)
            {
                var ret = rege.Match(wndName[i]);
                if (ret.Success)
                {
                    if (ret.Value.Trim() != "null")
                        wndName[i] = ret.Value.Trim();
                    else
                        wndName[i] = null;
                }
                  
            }
            className= System.Configuration.ConfigurationManager.AppSettings["classNameArray"].Split(',');
            for (int i = 0; i < className.Length; i++)
            {
                var ret = rege.Match(className[i]);
                if (ret.Success)
                    if (ret.Value.Trim() != "null")
                        className[i] = ret.Value.Trim();
                    else
                        className[i] = null;
            }
            log.AppendLine(DateTime.Now + " 读取句柄配置文件 完成");


            //就是这3个 ,按钮 边框透明, 鼠标在上面时 也不要加上白色的背景。
            btnExit.FlatAppearance.MouseOverBackColor = Color.Transparent;
            btnExit.FlatAppearance.MouseDownBackColor = Color.Transparent;
            btnExit.FlatAppearance.BorderColor = Color.FromArgb(216, 211, 197);
            log.AppendLine(DateTime.Now + " 窗口初始化 完成");
            ShowStartAnimation();
        }
        bool isPrompt_ForShowStartAnimation = false;
        private void ShowStartAnimation()
        {
            //这是一点问题都没有的写法, 已经证明了3次, 耗时1个月测试。
            //以后尽量 重用代码, 而不是 测试测试,(会记一大堆错误的测试结果, 导致我响应速度降低)。有经验是优势, 但过多经验却降 敏捷!
            Task.Run(() =>
            {
                Thread.Sleep(500); //暂停的是新线程
                var OrginPoint = new Point(DefShowWindowLocation.X + 100, DefShowWindowLocation.Y);
                for (int i = 1; i < 200; i++)
                {
                    this.Invoke(new Action(() =>
                    {
                        Location = new Point(OrginPoint.X - i * 3, OrginPoint.Y);
                    }));

                    if (Location.X < -251)
                    {
                        this.Invoke(new Action(() =>
                        {
                            Location = DefHideWindowLocation;
                        }));
                        break;
                    }
                    Application.DoEvents();
                    Thread.Sleep(1); //自动调度 真异步 与 假异步,( 因为逻辑核心只有4个, 这是操作系统决定是否采用真异步)。
                    
                }
                log.AppendLine(DateTime.Now + " 窗口缩进 屏幕外 完成");


                Stopwatch sw = new Stopwatch();
                sw.Restart();
                while (true)
                {
                    if (FrmClosing)
                    {
                        break;
                    }
                    this.Invoke(new Action(() => UI_Debug.Text = "等待中"));
                    if (isPrompt_ForShowStartAnimation)
                    {
                        this.Invoke(new Action(() => this.Location = DefShowWindowLocation));
                        //User32.SetWindowPos(Handle, 0, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);

                        if (!(MousePosition.Y > this.Location.Y && MousePosition.Y < this.Location.Y + this.Height && MousePosition.X < this.Width))
                        {
                            this.Invoke(new Action(()=> UI_Debug.Text = "在外")) ;
                            sw.Start();

                            if (sw.ElapsedMilliseconds > 100)
                            {
                                log.AppendLine(DateTime.Now + " 鼠标在窗口区域 外 未超过500ms 等待");
                                this.Invoke(new Action(() => UI_Debug.Text = "在外超时,隐藏"));
                                Thread.Sleep(400);
                                this.Invoke(new Action(() => this.Location = DefHideWindowLocation));
                                log.AppendLine(DateTime.Now + " 鼠标在窗口区域 外 超过500ms 隐藏");
                                isPrompt_ForShowStartAnimation = false;
                                sw.Reset();
                            }

                        }
                        else
                            sw.Stop();
                    }
                    else if (MousePosition.Y > DefShowWindowLocation.Y && MousePosition.Y < DefShowWindowLocation.Y + this.Height && MousePosition.X < 3)
                        this.Invoke(new Action(() =>
                        {
                            if (this.Location.X < 0)
                            {
                                this.Invoke(new Action(() => UI_Debug.Text = "在内,弹出"));
                                log.AppendLine(DateTime.Now + " 鼠标在屏幕边缘 弹出");
                                this.Location = DefShowWindowLocation;
                                isPrompt_ForShowStartAnimation = true;
                            }
                        }));

                    Thread.Sleep(30);
                    if (FrmClosing)
                        break;
                }
            });
        }

        enum MgrWindowName : int
        {
            组态王,
            报表浏览器,
            开动率管理系统,
            故障分析程序
        }

        string[] processName = { "Touchvew", "ReportSystem", "捻织开动率管理系统", "故障分析程序" };
        string[] wndName = { null, "一科报表查看器 ver 1.0.3", "捻织开动率管理系统", "故障分析程序" };
        string[] className = { "pingmei_view", "WindowsForms10.Window.8.app.0.13965fa_r6_ad1", "WindowsForms10.Window.208.app.0.141b42a_r10_ad1", "WindowsForms10.Window.8.app.0.141b42a_r9_ad1" };

        private void FindAllWindows(MgrWindowName mgrWindowName)
        {
            MemoryStream ms = new MemoryStream();

            IntPtr AnyHandle = new IntPtr(0);
            AnyHandle = FindWindow(className[(int)mgrWindowName], wndName[(int)mgrWindowName]);
            if (AnyHandle == IntPtr.Zero)
            {
                _MsgBox.ShowDialog("请检查【" + mgrWindowName.ToString() + "】是否运行。");
            }
            else
            {

                //-------------------根据窗口句柄获取进程ID------------------------
                //----------------禁用的原因是因为，全部自己用图标代替了---------------
                //int pid = 0;
                //if (TaskMGR.WinAPI_GetWindowThreadProcessId(AnyHandle, out pid) != 0)
                //{
                //    var icon = TaskMGR.GetLargeIconFromPID(pid);

                //    icon?.ToBitmap().Save(ms, ImageFormat.Png);
                //}
                //------------------------------------------------------------
                //if (AnyHandle != IntPtr.Zero)
                //{
                //    continue;
                //}
                //突破性读取  文件图标 方法，居然保留了 透明信息！！！
                {
                    switch ((int)mgrWindowName)
                    {
                        case 0:
                            //切换组态王ToolStripMenuItem.Image = Image.FromStream(ms);
                            t1_切换组态王ToolStripMenuItem.Tag = AnyHandle;
                            break;
                        case 1:
                            //t2_切换报表查看器ToolStripMenuItem.Image = Image.FromStream(ms);
                            t2_切换报表查看器ToolStripMenuItem.Tag = AnyHandle;

                            break;
                        case 2:
                            //if (ms.Length != 0)
                                //t3_开动率管理系统ToolStripMenuItem.Image = Image.FromStream(ms);
                            t3_开动率管理系统ToolStripMenuItem.Tag = AnyHandle;
                            break;
                        case 3:
                            //if (ms.Length != 0)
                            //    t4_故障分析程序ToolStripMenuItem.Image = Image.FromStream(ms);
                            t4_故障分析程序ToolStripMenuItem.Tag = AnyHandle;
                            break;

                        default:
                            break;
                    }
                }

            }

        }

        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", EntryPoint = "FindWindowW", CharSet = CharSet.Auto)]
        private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        private static extern Boolean SetForegroundWindow(IntPtr hWnd);


        [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        public int WindowsState_Max = 3;
        public int WindowsState_Min = 2;
        public int WindowsState_Normal = 1;
        private void button1_Click(object sender, EventArgs e)
        {

            return;
            IntPtr DesktopHandle = new IntPtr(0);
            SetForegroundWindow(DesktopHandle);
            ShowWindow(DesktopHandle, WindowsState_Max);

            DesktopHandle = FindWindow(null, "Form1");


            //判断这个窗体是否有效 
            if (DesktopHandle != IntPtr.Zero)
            {
                MessageBox.Show("找到向日葵窗口");
                SetForegroundWindow(DesktopHandle);
                ShowWindow(DesktopHandle, WindowsState_Min);
                System.Threading.Thread.Sleep(800);
                ShowWindow(DesktopHandle, WindowsState_Max);

                System.Threading.Thread.Sleep(800);
                ShowWindow(DesktopHandle, WindowsState_Normal);


            }
            else
                MessageBox.Show("没有找到窗口");
        }

        bool FrmClosing = false;
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            FrmClosing = true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定 关闭程序吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                this.Close();
        }

        private void btnExit_MouseLeave(object sender, EventArgs e)
        {
            this.btnExit.ImageIndex = 1;
        }


        private void btnExit_MouseEnter(object sender, EventArgs e)
        {
            this.btnExit.ImageIndex = 0;
            
        }

        private void 切换组态王ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            FindAllWindows(MgrWindowName.组态王);
            if (t1_切换组态王ToolStripMenuItem.Tag != null)
            {
                if (isMainSysShow)
                {
                    log.AppendLine(DateTime.Now + " 【组态王】窗口还原 最大化 开始");
                    SetForegroundWindow((IntPtr)t1_切换组态王ToolStripMenuItem.Tag);
                    Thread.Sleep(50);
                    ShowWindow((IntPtr)t1_切换组态王ToolStripMenuItem.Tag, WindowsState_Max);
                    isMainSysShow = false;
                    log.AppendLine(DateTime.Now + " 【组态王】窗口还原 最大化 完成");
                    //User32.SetWindowPos((IntPtr)切换组态王ToolStripMenuItem.Tag, 0, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
                }
                else
                {
                    ShowWindow((IntPtr)t1_切换组态王ToolStripMenuItem.Tag, WindowsState_Min);
                    isMainSysShow = true;
                    log.AppendLine(DateTime.Now + " 【组态王】窗口还原 最小化 完成");
                }

            }
        }


        bool isMainSysShow = true;
        bool isReportSysShow = true;
        private void 切换报表查看器ToolStripMenuItem_Click(object sender, EventArgs e)
        {


            FindAllWindows(MgrWindowName.报表浏览器);
            if (t2_切换报表查看器ToolStripMenuItem.Tag != null)
            {
                if (isReportSysShow)
                {
                    //(IntPtr)切换报表查看器ToolStripMenuItem.Tag
                    log.AppendLine(DateTime.Now + " 【报表查看器】窗口还原 最大化 开始");
                    SetForegroundWindow((IntPtr)t2_切换报表查看器ToolStripMenuItem.Tag);
                    Thread.Sleep(50);
                    ShowWindow((IntPtr)t2_切换报表查看器ToolStripMenuItem.Tag, WindowsState_Max);
                    isReportSysShow = false;
                    //ShowWindow((IntPtr)切换报表查看器ToolStripMenuItem.Tag, WindowsState_Normal);
                    //User32.SetWindowPos((IntPtr)切换报表查看器ToolStripMenuItem.Tag, 0, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
                    log.AppendLine(DateTime.Now + " 【报表查看器】窗口还原 最大化 成功");
                }
                else
                {
                    log.AppendLine(DateTime.Now + " 【报表查看器】窗口还原 最小化 开始");
                    ShowWindow((IntPtr)t2_切换报表查看器ToolStripMenuItem.Tag, WindowsState_Min);
                    isReportSysShow = true;
                    log.AppendLine(DateTime.Now + " 【报表查看器】窗口还原 最小化 成功");
                }
            }
        }

        bool isUptimeRateShow = true;
        private void 开动率管理系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FindAllWindows(MgrWindowName.开动率管理系统);
            if (t3_开动率管理系统ToolStripMenuItem.Tag != null)
            {
                if (isUptimeRateShow)
                {
                    //(IntPtr)切换报表查看器ToolStripMenuItem.Tag
                    log.AppendLine(DateTime.Now + " 【开动率管理系统】窗口还原 最大化 开始");
                    SetForegroundWindow((IntPtr)t3_开动率管理系统ToolStripMenuItem.Tag);
                    Thread.Sleep(50);
                    ShowWindow((IntPtr)t3_开动率管理系统ToolStripMenuItem.Tag, WindowsState_Max);
                    isUptimeRateShow = false;
                    //ShowWindow((IntPtr)切换报表查看器ToolStripMenuItem.Tag, WindowsState_Normal);
                    //User32.SetWindowPos((IntPtr)切换报表查看器ToolStripMenuItem.Tag, 0, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
                    log.AppendLine(DateTime.Now + " 【开动率管理系统】窗口还原 最大化 完成");
                }
                else
                {
                    ShowWindow((IntPtr)t3_开动率管理系统ToolStripMenuItem.Tag, WindowsState_Min);
                    isUptimeRateShow = true;
                }
            }
        }



        bool isFaultsAnalysisShow = true;
        private void t4_故障分析程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FindAllWindows(MgrWindowName.故障分析程序);
            log.AppendLine(DateTime.Now + " 查找【故障分析程序】窗口成功");
            if (t4_故障分析程序ToolStripMenuItem.Tag != null)
            {
                if (isFaultsAnalysisShow)
                {
                    //(IntPtr)切换报表查看器ToolStripMenuItem.Tag
                    log.AppendLine(DateTime.Now + " 故障分析程序(普通) 开始");
                    Task.Run(() =>
                    {
                        SetForegroundWindow((IntPtr)t4_故障分析程序ToolStripMenuItem.Tag);
                        ShowWindow((IntPtr)t4_故障分析程序ToolStripMenuItem.Tag, WindowsState_Normal);
                        Thread.Sleep(2000);
                    });
                    Thread.Sleep(1000);

                    isFaultsAnalysisShow = false;
                    //ShowWindow((IntPtr)切换报表查看器ToolStripMenuItem.Tag, WindowsState_Normal);
                    //User32.SetWindowPos((IntPtr)切换报表查看器ToolStripMenuItem.Tag, 0, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
                    log.AppendLine(DateTime.Now + " 故障分析程序(普通) 完成");
                }
                else
                {
                    log.AppendLine(DateTime.Now+ "故障分析程序(最小化) 开始");
                    ShowWindow((IntPtr)t4_故障分析程序ToolStripMenuItem.Tag, WindowsState_Min);
                    isFaultsAnalysisShow = true;
                    log.AppendLine(DateTime.Now + " 故障分析程序(最小化) 完成");
                }
            }
        }


        public void SYNCDelay(int ms)
        {
            Stopwatch sw1 = new Stopwatch();
            sw1.Start();
            while (true)
            {
                if (sw1.ElapsedMilliseconds > ms)
                    break;
                Application.DoEvents();
                Thread.Sleep(10);
            }
        }

        private void 桌面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Type oleType = Type.GetTypeFromProgID("Shell.Application");
            object oleObject = System.Activator.CreateInstance(oleType);
            Thread.Sleep(40);
            oleType.InvokeMember("ToggleDesktop", BindingFlags.InvokeMethod, null, oleObject, null);//报错地方自行引用空间名
            Thread.Sleep(100);
            log.AppendLine(DateTime.Now + " 切换桌面");
        }
        StringBuilder log = new StringBuilder(1000);
        FrmLog frmLog = new FrmLog();
        bool BtnLogFlag = false;
        private void BtnLog_Click(object sender, EventArgs e)
        {
            if(BtnLogFlag)
            {
                frmLog.Hide();
                BtnLogFlag = false;
            }else
            {
                frmLog.tbx.AppendText(log.ToString());
                frmLog.Location = new Point(180, 518);
                frmLog.Show();
                frmLog.tbx.SelectionStart = frmLog.tbx.Text.Length;
                frmLog.tbx.ScrollToCaret();


                BtnLogFlag = true;
            }
            
        }

        private void BtnManual_Click(object sender, EventArgs e)
        {
                this.Location = DefHideWindowLocation;
                isPrompt_ForShowStartAnimation = false;
        }
    }

    class TaskMGR
    {
        private const int MAX_PATH = 260;
        public const int PROCESS_ALL_ACCESS = 0x000F0000 | 0x00100000 | 0xFFF;
        [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
        public extern static int WinAPI_GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
        [DllImport("Kernel32.dll")]
        public extern static IntPtr OpenProcess(int fdwAccess, int fInherit, int IDProcess);
        [DllImport("Kernel32.dll")]
        public extern static bool TerminateProcess(IntPtr hProcess, int uExitCode);
        [DllImport("Kernel32.dll")]
        public extern static bool CloseHandle(IntPtr hObject);

        [DllImport("Kernel32.dll", EntryPoint = "GetModuleFileNameW", CharSet = CharSet.Unicode)]
        private static extern uint GetModuleFileName(IntPtr hModule, [Out] StringBuilder lpszFileName, int nSize);


        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="index">必须为0</param>
        /// <param name="hIconLarge"></param>
        /// <param name="hIconSmall"></param>
        /// <param name="nIcons">1:是最大的; 2:是小图</param>
        /// <returns></returns>
        [DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        private static extern IntPtr ExtractIconEx(string fileName, int index, ref IntPtr hIconLarge, ref IntPtr hIconSmall, uint nIcons);
        [DllImport("user32.dll")]
        static extern IntPtr GetTopWindow(IntPtr hWnd);
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
        enum GetWindow_Cmd : uint
        {
            GW_HWNDFIRST = 0,
            GW_HWNDLAST = 1,
            GW_HWNDNEXT = 2,
            GW_HWNDPREV = 3,
            GW_OWNER = 4,
            GW_CHILD = 5,
            GW_ENABLEDPOPUP = 6
        }
        //根据进程ID获取窗口句柄
        public static IntPtr GetHwndByPid(int dwProcessID)
        {
            //返回Z序顶部的窗口句柄
            IntPtr hWnd = GetTopWindow(IntPtr.Zero);


            while (hWnd != IntPtr.Zero)
            {
                int pid = 0;
                //根据窗口句柄获取进程ID
                int dwTheardId = WinAPI_GetWindowThreadProcessId(hWnd, out pid);


                if (dwTheardId != 0)
                {
                    if (pid == dwProcessID)
                    {
                        return hWnd;
                    }
                }
                //返回z序中的前一个或后一个窗口的句柄
                hWnd = GetWindow(hWnd, (uint)GetWindow_Cmd.GW_HWNDNEXT);
            }
            return hWnd;
        }

        //根据句柄获取运行程序小图标 //当然也可以获取大图标

        public static Icon GetSmallIconFromHandle(int PID)
        {
            IntPtr hLargeIcon = IntPtr.Zero;
            IntPtr hSmallIcon = IntPtr.Zero;

            string filePath = Process.GetProcessById(PID).MainModule.FileName;
            ExtractIconEx(filePath, 0, ref hLargeIcon, ref hSmallIcon, 2);
            Icon icon = null;
            try
            {
                icon = (Icon)Icon.FromHandle(hSmallIcon);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw;
            }
            return icon;
        }
        public static Icon GetLargeIconFromPID(int PID)
        {
            IntPtr hLargeIcon = IntPtr.Zero;
            IntPtr hSmallIcon = IntPtr.Zero;

            string filePath = Process.GetProcessById(PID).MainModule.FileName;
            //MessageBox.Show(filePath);
            ExtractIconEx(filePath, 0, ref hLargeIcon, ref hSmallIcon, 1);
            Icon icon = null; 
            try
            {
                if (hLargeIcon != IntPtr.Zero)
                {
                    icon = Icon.FromHandle(hLargeIcon);
                }
                if(icon==null)
                {
                    if(hSmallIcon != IntPtr.Zero)
                        icon = Icon.FromHandle(hSmallIcon);
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                throw;
            }
            return icon;
        }
    }
    internal class User32
    {
        public const int SE_SHUTDOWN_PRIVILEGE = 0x13;
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll")]
        public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    }
}
